package SceneTrigger

import (
	. "GameLogic/Scene"
	. "GameLogic/Scene/Unit"
	"fmt"
	. "misc/Clojure"
	. "misc/Clojure/parse"
	. "utils"
)

func defineClojureActions() {
	MainContext.Define("CreateUnit", NativeFunction{MyFunction: _Create_Unit})
	MainContext.Define("create-starting-units", NativeFunction{MyFunction: _Create_Starting_Units})
	MainContext.Define("lock-camera-to-unit", NativeFunction{MyFunction: _do_nothing})
	MainContext.Define("localplayer", NativeFunction{MyFunction: _do_nothing})
	MainContext.Define("player-main-unit", NativeFunction{MyFunction: _do_nothing})
	MainContext.Define("vec3", NativeFunction{MyFunction: _vec3})

	MainContext.Define("unit-flag", NativeFunction{MyFunction: _unit_flag})
	MainContext.Define("trigger-unit", NativeFunction{MyFunction: _trigger_unit})
	MainContext.Define("revive-unit", NativeFunction{MyFunction: _revive_unit})
	MainContext.Define("move-unit", NativeFunction{MyFunction: _move_unit})
	//Condition
	defineTriggerVarAction()
}
func _do_nothing(args []Node, context *Context) Node {
	return nil
}

func _vec3(args []Node, context *Context) Node {
	args = EvalArgs(args, context)
	x := args[0].(*NumberNode).Val.(float64)
	y := args[1].(*NumberNode).Val.(float64)
	z := args[2].(*NumberNode).Val.(float64)
	return &ObjectNode{Val: Vec3{x, y, z}}
}

///////////////////////Unit////////////////////////////////////////
func _Create_Unit(args []Node, context *Context) Node {
	fmt.Println("_Create_Unit CNM KaLuBin")
	return nil
}

func _Create_Starting_Units(args []Node, context *Context) Node {

	CurScene := context.LookUp("CurScene").(*ObjectNode).Val.(*Scene)
	for i := 0; i < len(CurScene.GamePlayers); i++ {
		gameplayer := CurScene.GamePlayers[i]
		unit := CurScene.CreateUnit(gameplayer.SelectUnitName, i, gameplayer.UserName)
		unit.GetBaseUnit().Pos = Vec3{30, 0, 30}
	}
	return nil
}

func _revive_unit(args []Node, context *Context) Node {
	args = EvalArgs(args, context)
	unit := args[0].(*ObjectNode).Val.(*Unit)

	unit.HP = unit.MaxHP
	unit.MP = unit.MaxMP
	unit.IsDie = false
	return nil
}

func _move_unit(args []Node, context *Context) Node {
	args = EvalArgs(args, context)
	unit := args[0].(*ObjectNode).Val.(*Unit)
	vec3 := args[1].(*ObjectNode).Val.(Vec3)
	unit.Pos = vec3
	return nil
}

func _unit_flag(args []Node, context *Context) Node {
	args = EvalArgs(args, context)
	unit := args[0].(*ObjectNode).Val.(*Unit)
	flagkey := args[1].(*StringNode).Val
	return &BoolNode{Val: unit.Flag(flagkey)}
}

func _trigger_unit(args []Node, context *Context) Node {

	triggerUnit := context.LookUp("TriggerUnit").(*ObjectNode).Val.(IUnit)

	return &ObjectNode{Val: triggerUnit}
}
